<!--
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2022-05-06 00:55:43
 * @LastEditors: Sam
 * @LastEditTime: 2022-05-06 02:12:34
-->
<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>题目管理</span>
      </div>

      <div class="filter-container">
        <el-input v-model="listQuery.title" :placeholder="'请输入关键字'" clearable style="width: 200px;margin-right: 10px;" class="filter-item" @keyup.enter.native="handleFilter" />


        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
          {{ '搜索' }}
        </el-button>
        <el-button v-waves :disabled="!checkPermission(['question.create'])" class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
          {{ '添加' }}
        </el-button>
        <el-button v-waves :disabled="!checkPermission(['question.delete'])" class="filter-item" type="danger" icon="el-icon-delete" @click="handleBatch">
          {{ '批量删除' }}
        </el-button>
    
         
      </div>

      <el-table
        v-loading="listLoading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="list"
        border
        fit
        @select="handleSelect"
        @select-all ="selectAll"
        highlight-current-row
        style="width: 100%"
        row-key="id"
        :tree-props="{children: 'children'}"
      >
        <el-table-column
            align="center"
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column min-width="50" :label="'编号'" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="100px" :label="'类型'" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.type_text}}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="100px" :label="'主题'" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.survey_title}}</span>
          </template>
        </el-table-column>
    
        <el-table-column min-width="100px" :label="'题目'" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.title }}</span>
          </template>
        </el-table-column>


        <el-table-column min-width="150px" align="center" :label="'添加时间'">
          <template slot-scope="scope">
            <span>{{ scope.row.create_time | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" :label="'操作'" width="350">
          <template slot-scope="scope">
            <el-button :disabled="!checkPermission(['question.edit'])" type="primary" size="mini" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">
              {{ '编辑' }}
            </el-button>
            <el-button v-permission="['question.delete']" type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.$index, scope.row)">
              {{ '删除' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </el-card>

    <el-dialog :title="'添加题目'" :visible.sync="create.dialogVisible" @close="getList" :close-on-click-modal=false>
      <create  :survey_list="survey_list" :item="create" />
    </el-dialog>

    <el-dialog :title="'编辑题目'" :visible.sync="edit.dialogVisible" @close="getList" :close-on-click-modal=false>
      <edit  :survey_list="survey_list" :item="edit" />
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import permission from '@/directive/permission/index.js' // 权限判断指令
import checkPermission from '@/utils/permission' // 权限判断函数
import edit from './components/edit'
import { getToken } from '@/utils/auth'
import create from './components/create'
import { list,del } from '@/api/survey_question'
import { parseTime } from '@/utils'
export default {
  name: 'question_index',
  components: { Pagination, edit, create },
  directives: { waves, permission },
  filters: {
    parseTime
  },
  data() {
    return {

      list: [],
      survey_list: [],
      total: 0,
      select_ids:[],
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      create: {
        dialogVisible: false
      },
      edit: {
        dialogVisible: false,
        id: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {

    beforeImport(){

        this.listLoading = true;
    },
    handleImport(res) { // 导入
        if(!res.code) {
            this.$message.error(res.msg)
            this.listLoading = false;
        } else {
            this.listLoading = false;
            this.$message.success(res.msg);
            this.getList()
        }
    },
    handleSelect(selection, row) {
          this.select_ids = [];
        selection.map(item => {
              this.select_ids.push(item.id);
        })
    },
      selectAll(selection) {
          this.select_ids = [];
        selection.map(item => {
              this.select_ids.push(item.id);
        })
    },
    checkPermission,
    getList() {
      this.listLoading = true
      list(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.survey_list = response.data.survey
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.create.dialogVisible = true
    },
    handleEdit(index, row) {
      this.edit.dialogVisible = true
      this.edit.id = row.id
    },
  
    handleBatch() {
        var that = this;
        if(this.select_ids.length == 0){
            this.$message({
                message: '请选择要删除的信息',
                type: 'warning'
            });
            return false;
        }
        this.$confirm('确认删除选中的数据?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            del(this.select_ids.join(',')).then(res => {
                this.$message({
                    message: '删除成功',
                    type: 'success'
                });
                this.getList();
            }).catch(err => {
                this.$message({
                    message: err.msg,
                    type: 'error'
                });
            })
        }).catch(() => {
            this.$message({
                type: 'info',
                message: '已取消删除'
            });
        });
    },
    handleDelete(index, row) {
      const thiz = this
      this.$confirm('确认要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.id).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1500,
            onClose() {
              thiz.list.splice(index, 1)
            }
          })
        })
      }).catch(() => {

      })
    }
  }
}
</script>

<style scoped>
.pagination-container {
  padding: 5px 2px;
}
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
